import VueRouter from "vue-router";
import Home from "@/pages/Home/Home";
import About from "@/pages/About";
import News from "@/pages/Home/News";
import Message from "@/pages/Home/Message/Message";
import Detail from "@/pages/Home/Message/Detail";

const router = new VueRouter({
    routes: [
        {
            name: 'Home',
            path: '/Home',
            component: Home,
            meta: {
                title: 'Home'
            },
            children: [
                {
                    name: 'News',
                    path: 'News',
                    component: News,
                    meta: {isAuth: true, title: 'News'},
                },
                {
                    name: 'Message',
                    path: 'Message',
                    component: Message,
                    meta: {isAuth: true, title:'Message'},
                    children: [
                        {
                            name: 'Detail',
                            path: 'Detail',
                            component: Detail,
                            meta: {isAuth: true, title:'Detail'},
                            props($route) {
                                return {
                                    id: $route.query.id,
                                    title: $route.query.title
                                }
                            },
                        },
                    ]
                },
            ]
        },
        {
            name: 'About',
            path: '/About',
            component: About,
            meta:{title:'About'}
        },
    ]
});

// 全局前置路由守卫：在初始化及每一次路由切换之前被调用
router.beforeEach((to, from, next) => {
    if (to.meta.isAuth) {
        if (localStorage.getItem('school') === 'abc') {
            next();
        } else {
            console.log("学校名称不对，无权限查看！");
        }
    } else {
        next();
    }
});

// 全局后置路由守卫：在初始化及每一次路由切换之后被调用
router.afterEach((to, from) => {
    document.title = to.meta.title || 'Home';
});

export default router;